﻿@page "/ShareResult/{cipher}"
@using Tmp.Services
@using Microsoft.Extensions.Options
@using Tmp.Models
@using Neodynamic.Blazor.BarcodeProfessional
@using Microsoft.AspNetCore.WebUtilities
@inject NotificationService NotificationService
@inject ClipboardService Clipboard
@inject IOptions<Configuration> Opts

<RadzenCard class="m-2">
    <RadzenText>分享成功</RadzenText>
    <RadzenStack Orientation="Orientation.Horizontal">
        <RadzenButton Click="GoesTo" ButtonStyle="ButtonStyle.Primary">前往</RadzenButton>
        <RadzenButton Click="CopyLink" ButtonStyle="ButtonStyle.Secondary">复制链接</RadzenButton>
    </RadzenStack>
    <RadzenCard>
        <RadzenTextBox @ref="box" Value="@_text" class="col-5" />
        <RadzenButton ButtonStyle="ButtonStyle.Secondary" Click="CopyText">复制</RadzenButton>
        <RadzenButton ButtonStyle="ButtonStyle.Secondary" Click="CopyAndGoto">复制并前往草料网制作二维码</RadzenButton>
    </RadzenCard>
    <RadzenCard class="m-2">
        <BarcodeProfessional Code="@_text"
                             Symbology="Symbology.QRCode" Text="@_text"
                             QRCodeModuleSize="0.0617" BarcodeUnit="BarcodeUnit.Inch"
                             Width="400" Height="400"
                             QRCodeEncoding="QRCodeEncoding.Byte"
                             TextFontSizeInPoints="10"></BarcodeProfessional>
        </RadzenCard>
</RadzenCard>

@code {


    RadzenTextBox box;
    public string? ShareType { get; set; }

    [Parameter]
    public string? Cipher { get; set; }
    [Inject]
    public NavigationManager? Manager { get; set; }

    private void GoesTo()
    {
        Manager?.NavigateTo($"/{ShareType}/{Cipher}");
    }

    private string? _text;
    protected override Task OnInitializedAsync()
    {
        var uri = new Uri(Manager.Uri);
        var queryParams = QueryHelpers.ParseQuery(uri.Query);

        if (queryParams.TryGetValue("type", out var shareType))
        {
            ShareType = shareType;
    // 其他逻辑...
        }
        _text = $"{Opts.Value.Site}/{ShareType}/{Cipher}";
        return Task.CompletedTask;
    }
    private async Task CopyLink()
    {
        await Clipboard.SetClipboardText(_text??"");
        ShowNotification(new NotificationMessage {
            Severity = NotificationSeverity.Success, Summary = "复制成功",
            Detail = $"复制内容{_text}成功", Duration = 4000 });
    }
    void ShowNotification(NotificationMessage message)
    {
        NotificationService.Notify(message);
    }

    private async Task CopyText()
    {
        await Clipboard.SetClipboardText(box.Value);
        ShowNotification(new NotificationMessage {
            Severity = NotificationSeverity.Success, Summary = "复制成功",
            Detail = $"复制内容{box.Value}成功", Duration = 4000 });
    }

    private async Task CopyAndGoto()
    {
        await Clipboard.SetClipboardText(box.Value);
        ShowNotification(new NotificationMessage {
            Severity = NotificationSeverity.Success, Summary = "复制成功",
            Detail = $"复制内容{box.Value}成功", Duration = 4000 });
        Manager.NavigateTo("https://cli.im/text");
    }

}